package com.msti.utility;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;


public class Utility {
	static long id =0;
	public static Date stringToDate(String s)
	{
		DateFormat df = new SimpleDateFormat("dd-MMM-yy");
		Date date = null;
		try {
			date = df.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String getStudentId(long tempId){
		String stdId = null;
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		stdId = year + "MSTI" + tempId;
		return stdId;
	}
	
	public static String getBatchId(int tempBatchId){
		String batchId = "B" + tempBatchId;
		return batchId;
	}
	
	public static String getCertificateId(String batchId, int idHelper, 
			int coursType, String CourseShortName){
		String id ="MSTI-";
		if(coursType == 0)
			id += "OTC/";
		if(coursType == 1)
			id += "RTC/";
		id += CourseShortName + "/" + batchId + idHelper;
		return id;
		
	}
	
	public static synchronized void writeId(int hid, String fP){
		
		try {
			FileWriter fstream = new FileWriter(fP);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(Integer.toString(hid));
			out.close();
		} catch (Exception e) {
		}
	}
	
	public static synchronized int getId(String fP){
		String s="0";
		try {
			FileReader fileReader = new FileReader(fP);
			BufferedReader read = new BufferedReader(fileReader);
			s = read.readLine();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Integer.parseInt(s)+1;
	}
	


	public static long getMappingDateAsString(Date d) {
		String s = Integer.toString(1900+d.getYear());
		s += Integer.toString(d.getMonth()+1);
		s += Integer.toString(d.getDate());
		return Long.parseLong(s);
	}
	
	public static long getUniqueId(){
		id++;
		return id;
	}
}
